package com.erp.module.faHuoDanJu.faHuo1

import android.view.View
import androidx.appcompat.app.AlertDialog
import com.erp.activity.BaseActivity
import com.erp.myAdapter.DanjuAdapter
import com.erp.bean.DanjuBean
import com.erp.Interface.DisplayElemrnt
import com.erp.module.faHuoDanJu.FaHuoDanJuContract
import com.erp.R
import com.erp.util.Myutil
import com.erp.view.PullToRefreshView
import kotlinx.android.synthetic.main.activity_myfahuo.*
import kotlinx.android.synthetic.main.header_knoya.*
import java.util.*

/**
 * Created by Administrator on 2017/2/9/009.
 * 作者：刘洋
 */
class MyFahuoActivity : BaseActivity(), PullToRefreshView.OnFooterRefreshListener, View.OnClickListener,FaHuoDanJuContract.MyFaHuoView, PullToRefreshView.OnHeaderRefreshListener {



    /**
     * 单据的数据
     */
    lateinit var danjuBeanList: MutableList<DanjuBean>
    /**
     * 单据的adapter
     */
    lateinit var danjuAdapter: DanjuAdapter
    /**
     * 搜索的专卖店名和包号
     */
    private var zhuanmaidian = ""
    private var baohao = ""
    private var page: Int = 0
    private var pagesize: Int = 0
    var updialog: AlertDialog?=null
    private var type = 0
    lateinit var P : IMyFaHuoP
    private val a = "MyFahuoActivity"
    override fun getLayoutId(): Int {
        activityname = a
        return R.layout.activity_myfahuo
    }

    override fun initCreate() {
        type = intent.getIntExtra("type", type)
        page = 0
        pagesize = 10
        danjuBeanList = ArrayList<DanjuBean>()
        danjuAdapter = DanjuAdapter(this, danjuBeanList, type, DisplayElemrnt {
            page = 0
            updialog = Myutil.showupdatedialog(this@MyFahuoActivity, "正在刷新数据", false)
            P.xiala()
        })
        LvDanju.adapter = danjuAdapter
        if (type == FragmentFahuo.DANJU) {
            tv_header_title.text = "物流单据"
        } else if (type == FragmentFahuo.QINGDAN) {
            tv_header_title.text = "发货清单"
        }
        iv_header_left.visibility = View.VISIBLE
        Pull.setheader(true)
        Pull.setOnHeaderRefreshListener(this)
        Pull.setOnFooterRefreshListener(this)
        iv_header_left.setOnClickListener(this)
        IvQingkong.setOnClickListener(this)
        IvSousuo.setOnClickListener(this)
        P=IMyFaHuoP(this,this,danjuBeanList,type)
        Pull.headerRefreshing()
        P.xiala()
    }

    override fun onHeaderRefresh(view: PullToRefreshView?) {
        P.xiala()
    }

    override fun onFooterRefresh(view: PullToRefreshView?) {
        P.shangla()
    }

    override fun onClick(v: View) {
        when (v) {
            IvQingkong -> {
                zhuanmaidian = ""
                baohao = ""
                EtZhuanmaidian.setText("")
                EtBaohao.setText("")
                updialog = Myutil.showupdatedialog(this@MyFahuoActivity, "正在刷新数据", false)
                P.xiala()
            }
            IvSousuo -> {
                zhuanmaidian = EtZhuanmaidian!!.text.toString()
                baohao = EtBaohao.text.toString()
                updialog = Myutil.showupdatedialog(this@MyFahuoActivity, "正在刷新数据", false)
                Myutil.notjianpan(this@MyFahuoActivity, IvQingkong)
                P.sousuo(zhuanmaidian,baohao)
            }
            iv_header_left -> {
                finish()
            }
        }
    }

    override fun xialaOk() {
        updialog?.dismiss()
        danjuAdapter.notifyDataSetChanged()
        Pull.onHeaderRefreshComplete()
    }

    override fun xialaNo() {
        updialog?.dismiss()
        Pull.onHeaderRefreshComplete()
    }

    override fun shanglaOk() {
        danjuAdapter.notifyDataSetChanged()
        Pull.onFooterRefreshComplete()
    }

    override fun shanglaNo() {
        Pull.onFooterRefreshComplete()
    }

    override fun gainDataOk() {
        danjuAdapter.notifyDataSetChanged()
    }
}
